import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../config/public_text.dart';
import '../../foundation/style/bold_text_style.dart';
import '../../model/key_value_model.dart';

/// FileName action_sheet
///
/// @Author lax
/// @Date 2024/4/3
///
/// @Description

class ActionSheet extends StatelessWidget {
  static Future<KeyValueModel?> show({
    required BuildContext context,
    List<KeyValueModel> dataSource = const [],
    String? title,
    TextStyle? titleStyle,
    TextStyle? itemStyle,
    TextStyle? cancelStyle,
    Function(KeyValueModel value)? onSelected,
  }) async {
    FocusScope.of(context).requestFocus(FocusNode());
    final value = await showCupertinoModalPopup(
      context: context,
      builder: (context) {
        return ActionSheet(
          dataSource: dataSource,
          title: title,
          titleStyle: titleStyle,
          itemStyle: itemStyle,
          cancelStyle: cancelStyle,
        );
      },
    );
    if (value is KeyValueModel && onSelected != null) {
      onSelected(value);
    }
    return value;
  }

  static Future<KeyValueModel?> showModal({
    required BuildContext context,
    List<KeyValueModel> dataSource = const [],
    String? title,
    TextStyle? titleStyle,
    TextStyle? itemStyle,
    TextStyle? cancelStyle,
    Function(KeyValueModel value)? onSelectItem,
  }) async {
    FocusScope.of(context).requestFocus(FocusNode());
    final value = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return ActionSheet(
          dataSource: dataSource,
          title: title,
          titleStyle: titleStyle,
          itemStyle: itemStyle,
          cancelStyle: cancelStyle,
        );
      },
    );
    if (value is KeyValueModel && onSelectItem != null) {
      onSelectItem(value);
    }
    return value;
  }

  final String? title;

  final TextStyle? titleStyle;
  final TextStyle? itemStyle;
  final TextStyle? cancelStyle;

  final List<KeyValueModel> dataSource;

  const ActionSheet({
    super.key,
    this.title,
    this.titleStyle,
    this.itemStyle,
    this.cancelStyle,
    this.dataSource = const [],
  });

  @override
  Widget build(BuildContext context) {
    return CupertinoActionSheet(
      title: title == null
          ? null
          : Text(
              title ?? '',
              style:
                  titleStyle ?? BoldTextStyle(color: PublicColor.lightText),
            ),
      actions: dataSource.map((e) {
        return CupertinoActionSheetAction(
          child: Text(
            e.value ?? '',
            style: itemStyle ?? BoldTextStyle(),
          ),
          onPressed: () {
            Navigator.pop(context, e);
          },
        );
      }).toList(),
      cancelButton: CupertinoActionSheetAction(
        child: Text(
          PublicText.cancel,
          style: cancelStyle ?? BoldTextStyle(color: PublicColor.primary),
        ),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }
}
